package com.zhaoxi.project.md.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhaoxi.framework.aspectj.lang.annotation.Log;
import com.zhaoxi.framework.aspectj.lang.enums.BusinessType;
import com.zhaoxi.project.md.domain.MdGoods;
import com.zhaoxi.project.md.service.impl.MdGoodsServiceImpl;
import com.zhaoxi.framework.web.controller.BaseController;
import com.zhaoxi.framework.web.domain.AjaxResult;
import com.zhaoxi.common.utils.poi.ExcelUtil;
import com.zhaoxi.framework.web.page.TableDataInfo;

/**
 * 产品Controller
 *
 * @author zhaoxi
 * @date 2023-07-22
 */
@RestController
@RequestMapping("/md/goods")
public class MdGoodsController extends BaseController {
    @Autowired
    private MdGoodsServiceImpl mdGoodsService;

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('md:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(MdGoods mdGoods) {
        startPage();
        List<MdGoods> list = mdGoodsService.selectMdGoodsList(mdGoods);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('md:goods:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdGoods mdGoods) {
        List<MdGoods> list = mdGoodsService.selectMdGoodsList(mdGoods);
        ExcelUtil<MdGoods> util = new ExcelUtil<MdGoods>(MdGoods.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('md:goods:query')")
    @GetMapping(value = "/{parentCode}")
    public AjaxResult getInfo(@PathVariable("parentCode") String parentCode) {
        return success(mdGoodsService.selectMdGoodsByParentCode(parentCode));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('md:goods:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdGoods mdGoods) {
        return toAjax(mdGoodsService.insertMdGoods(mdGoods));
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('md:goods:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdGoods mdGoods) {
        return toAjax(mdGoodsService.updateMdGoods(mdGoods));
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('md:goods:remove')")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{parentCodes}")
    public AjaxResult remove(@PathVariable String[] parentCodes) {
        return toAjax(mdGoodsService.deleteMdGoodsByParentCodes(parentCodes));
    }

    /**
     * 根据code删除产品多属性
     */
    @DeleteMapping("/delLine")
    public AjaxResult delLineByCode(@RequestBody String code) {
        return toAjax(mdGoodsService.deleteLineByCodes(code));
    }
    /**
     * 查询商品下面的属性值列表
     */
    @GetMapping("/listGoodsLines")
    public TableDataInfo listGoodsLines(String parentCode) {
        List<MdGoods> list = mdGoodsService.selectMdGoodsLinesList(parentCode);
        return getDataTable(list);
    }

    /**
     * 保存产品多属性的数据
     */
    @PostMapping("/saveMdGoodsLines")
    public AjaxResult saveMdGoodsLines(@RequestBody MdGoods mdGoods) {
        return toAjax(mdGoodsService.saveMdGoodsLines(mdGoods));
    }
}
